<?php

include_once('config/config.php');

$action = $_POST['action'] ?? '';;

if ($action === 'job') {
  $depid = $_POST['depid'] ?? '';

  $JobSql = "SELECT * FROM `pre_job` WHERE `depid` = '$depid' ORDER BY `id` DESC";

  $JobData = all($JobSql);

  if ($JobData) {
    echo json_encode(['code' => 1, 'msg' => '查询成功', 'data' => $JobData]);
    exit;
  } else {
    echo json_encode(['code' => 0, 'msg' => '暂无职位', 'data' => null]);
    exit;
  }
}

if ($action === 'PersonDel') {
  $id = $_POST['personid'] ?? 0;

  $person = find("SELECT * FROM `pre_person` WHERE `id` = '$id'");

  if (!$person) {
    echo json_encode(['code' => 0, 'msg' => '员工不存在', 'data' => null]);
    exit;
  }

  $result = del('person', "`id` = '$id'");

  if ($result['code'] === 1) {
    // 删除图片
    @is_file('.' . $person['avatar']) && unlink('.' . $person['avatar']);

    echo json_encode($result);
    exit;
  } else {
    echo json_encode($result);
    exit;
  }
}



if ($action === 'departmentDel') {
  $name = $_POST['departmentName'] ?? '';

  $department = find("SELECT * FROM `pre_department` WHERE `name` = '$name'");

  if (!$department) {
    echo json_encode(['code' => 0, 'msg' => '员工不存在', 'data' => null]);
    exit;
  }

  $result = del('department', "`name` = '$name'");

  if ($result['code'] === 1) {
    echo json_encode($result);
    exit;
  } else {
    echo json_encode($result);
    exit;
  }
}
